package com.lvwenguo.dao;

import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BasicDao<T> {
        private Connection connection;
        QueryRunner queryRunner=new QueryRunner();
        /*
        * 构造方法重写
        * */
     public BasicDao(Connection connection){
            this.connection=connection;
        }
    /*
    *查询单个数据，使用泛型
    * */
    public T selectOnt(String sql,Object[] params,Class<T> calsszz) throws Exception {

        T t=queryRunner.query(this.connection,sql,new BeanHandler<T>(calsszz),params);
        return t;
    }
    /*
    * 查询多个,使用泛型
    * */
    public List<T> selectList(String sql, Object[] params, Class<T> calsszz) throws Exception {

        List<T> list = queryRunner.query(this.connection, sql, new BeanListHandler<T>(calsszz), params);
        return list;
    }
    public List<T> selectList(String sql, Class<T> calsszz) throws Exception {


        List<T> list = queryRunner.query(this.connection, sql, new BeanListHandler<T>(calsszz));
        return list;
    }
    /*
    * 修改,使用泛型
    * */
    public int update(String sql,Object[] params) throws SQLException {

       return queryRunner.update(this.connection,sql,params);
    }
    /*
    * 新增
    * */
    public int Add(String sql,Object[] params) throws SQLException {

        return queryRunner.update(this.connection,sql,params);
    }

}
